package com.qianyu.project.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.IncomeContract;
import com.qianyu.project.entity.IncomeWorkAmount;
import com.qianyu.project.mapper.IncomeContractMapper;
import com.qianyu.project.service.IIncomeContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.project.service.IIncomeWorkAmountService;
import com.qianyu.project.utils.PageUtil;
import com.qianyu.project.utils.ResultTool;
import com.qianyu.project.vo.IncomeContractVo;
import com.qianyu.project.vo.IncomeReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IncomeContractServiceImpl extends ServiceImpl<IncomeContractMapper, IncomeContract> implements IIncomeContractService {

    @Autowired
    private IncomeContractMapper incomeContractMapper;
    @Autowired
    private IIncomeWorkAmountService incomeWorkAmountService;

    @Override
    public String getMaxId() {
        String code = incomeContractMapper.getMaxId();
        if(code == null){
            code = DateUtil.today().replaceAll("-", "") + "000";
        }
        return code;
    }

    @Transactional
    @FlowBegin("income_contract")
    @Override
    public Integer saveDetails(@FlowEntry("id") IncomeContract incomeForm, List<IncomeWorkAmount> incomeDetails) {
        incomeForm.setApproveStatus("1");
        incomeContractMapper.insert(incomeForm);
        incomeWorkAmountService.saveBatch(incomeDetails);
        return 1;
    }

    /**
     * 获取收入合同列表
     * @param params
     * @return
     */
    @Override
    public IPage<IncomeContractVo> getPageData(Map<String, String> params) {
        return incomeContractMapper.selectPageData(PageUtil.createPage(params), params);
    }

    @Override
    public IncomeContractVo selectOne(String id) {
        return incomeContractMapper.getOne(id);
    }

    @Override
    public IncomeContractVo selectByPid(String id) {
        return incomeContractMapper.selectByPid(id);
    }

    @Override
    public IPage<IncomeContractVo> selectInfo(Map<String, String> params) {
        return incomeContractMapper.selectInfo(PageUtil.createPage(params), params);
    }

    @Override
    public IPage<IncomeReportVo> selectReport(Map<String, String> params) {
        return incomeContractMapper.selectReport(PageUtil.createPage(params), params);
    }
}
